﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace DynamicChart.UI
{
    public partial class DateSlider : UserControl
    {
        private DynamicChart.UI.ServiceRef.DateRange _dateRange;

        public DynamicChart.UI.ServiceRef.DateRange DateRange
        {
            get
            {
                return _dateRange;
            }
        }

        private string _mode;

        private double _blockWidth;

        private int _startYear;
        private int _currentYear;
        private int _count;

        public event Action<string, DateTime> DateRangeChanged;
        public event Action<DynamicChart.UI.ServiceRef.DateRange> Enter;
        public event Action<DynamicChart.UI.ServiceRef.DateRange> ClickYear;

        public DateSlider()
        {
            InitializeComponent();

            this.LayoutRoot.SizeChanged += new SizeChangedEventHandler(LayoutRoot_SizeChanged);
        }

        void LayoutRoot_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            ReLayout();
        }

        //public void SetRangeYear(int currentYear, int backwardCount, int forwardCount)
        //{
        //    _startYear = currentYear - backwardCount;
        //    _currentYear = currentYear;
        //    _count = backwardCount + forwardCount + 1;
        //    ReLayout();

        //    if (DateRangeChanged != null)
        //    {
        //        DateRangeChanged(_currentYear);
        //    }
        //}

        public void SetDateRange(DynamicChart.UI.ServiceRef.DateRange dateRange)
        {
            _dateRange = dateRange;

            _mode = dateRange.Mode;

            if (_mode == "y")
            {
                _ctlBtnYear.Visibility = System.Windows.Visibility.Collapsed;
                _ctlBtnSelectMoreBackward.Visibility = System.Windows.Visibility.Visible;
                _ctlBtnSelectMoreForward.Visibility = System.Windows.Visibility.Visible;

                _startYear = dateRange.BeginDate.Year;
                _currentYear = dateRange.CurrentDate.Year;
                _count = dateRange.EndDate.Year - dateRange.BeginDate.Year + 1;
                ReLayout();
            }
            else if(_mode == "m")
            {
                _ctlBtnYear.Content = string.Format("{0}年", dateRange.CurrentDate.Year);
                _ctlBtnYear.Visibility = System.Windows.Visibility.Visible;
                _ctlBtnSelectMoreBackward.Visibility = System.Windows.Visibility.Collapsed;
                _ctlBtnSelectMoreForward.Visibility = System.Windows.Visibility.Collapsed;

                _startYear = 1;
                _currentYear = dateRange.CurrentDate.Month;
                _count = 12;
                ReLayout();
            }

            if (DateRangeChanged != null)
            {
                DateRangeChanged(_dateRange.Mode, _dateRange.CurrentDate);
            }
        }

        private void ReLayout()
        {
            if (_count == 0 && _startYear == 0)
            {
                return;
            }

            _blockWidth = this.LayoutRoot.ActualWidth / _count;

            if (LayoutRoot.Children.Count == _count)
            {
                for (int index = _startYear; index < _startYear + _count; index++)
                {
                    //TextBlock textBlock = new TextBlock();
                    //textBlock.Text = index.ToString();
                    //textBlock.TextAlignment = TextAlignment.Center;
                    //textBlock.Width = _blockWidth;
                    //textBlock.Height = 16;
                    SliderBlock sliderBlock = LayoutRoot.Children[index - _startYear] as SliderBlock;
                    sliderBlock.Height = LayoutRoot.Height;
                    sliderBlock.Width = _blockWidth;
                    Canvas.SetLeft(sliderBlock, (index - _startYear) * _blockWidth);
                    Canvas.SetTop(sliderBlock, 0);

                    if (index == _currentYear)
                    {
                        if (!sliderBlock.IsSelectedState)
                        {
                            sliderBlock.SelectedState();
                        }
                    }
                    else if (sliderBlock.IsSelectedState)
                    {
                        sliderBlock.UnSelectedState();
                    }

                }
            }
            else
            {
                LayoutRoot.Children.Clear();

                for (int index = _startYear; index < _startYear + _count; index++)
                {
                    string text = index.ToString();
                    if(_dateRange.Mode =="m")
                    {
                        text += "月";
                    }
                    SliderBlock sliderBlock = new SliderBlock(text);
                    sliderBlock.Height = LayoutRoot.Height;
                    sliderBlock.Width = _blockWidth;
                    Canvas.SetLeft(sliderBlock, (index - _startYear) * _blockWidth);
                    Canvas.SetTop(sliderBlock, 0);
                    LayoutRoot.Children.Add(sliderBlock);

                    if (index == _currentYear)
                    {
                        sliderBlock.SelectedState();
                    }
                }
            }
        }

        private void LayoutRoot_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Point pt = e.GetPosition(LayoutRoot);

            int index = (int)(pt.X / _blockWidth);

            if (_currentYear == _startYear + index)
            {
                if (!string.IsNullOrEmpty(_dateRange.NextMode))
                {
                    if (Enter != null)
                    {
                        DynamicChart.UI.ServiceRef.DateRange nextDateRange = new ServiceRef.DateRange();
                        nextDateRange.Mode = _dateRange.NextMode;
                        nextDateRange.BeginDate = _dateRange.BeginDate;
                        nextDateRange.CurrentDate = _dateRange.CurrentDate;
                        nextDateRange.EndDate = _dateRange.EndDate;
                        Enter(nextDateRange);
                    }
                }
            }
            else
            {
                if (_dateRange.Mode == "y")
                {
                    _dateRange.CurrentDate = _dateRange.BeginDate.AddYears(index);
                    _currentYear = _dateRange.CurrentDate.Year;
                }
                else if (_dateRange.Mode == "m")
                {
                    _dateRange.CurrentDate = new DateTime(_dateRange.CurrentDate.Year, index + 1, _dateRange.CurrentDate.Day);
                    _currentYear = _dateRange.CurrentDate.Month;
                }

                ReLayout();

                if (DateRangeChanged != null)
                {
                    DateRangeChanged(_dateRange.Mode, _dateRange.CurrentDate);
                }
            }
        }

        private void LayoutRoot_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            //if (e.Delta > 0)
            //{
            //    _currentYear = _currentYear + 1;
            //}
            //else
            //{
            //    _currentYear = _currentYear - 1;
            //}

            //ReLayout();

            //if (DateRangeChanged != null)
            //{
            //    DateRangeChanged(_currentYear);
            //}
        }

        private void _ctlBtnSelectMoreBackward_Click(object sender, RoutedEventArgs e)
        {
            if (_dateRange.Mode == "y")
            {
                Dialogs.SelectYearWindow selectYearWindow = new Dialogs.SelectYearWindow();
                selectYearWindow.Closed += new EventHandler(window_Closed);
                selectYearWindow.Show();
            }
        }

        void window_Closed(object sender, EventArgs e)
        {
            Dialogs.SelectYearWindow window = sender as Dialogs.SelectYearWindow;
            if (window.DialogResult == true)
            {
                if (window.SelectedYear != _currentYear)
                {
                    _dateRange.CurrentDate = new DateTime(
                            window.SelectedYear,
                            _dateRange.CurrentDate.Month,
                            _dateRange.CurrentDate.Day);
                    _currentYear = window.SelectedYear;

                    ReLayout();

                    if (DateRangeChanged != null)
                    {
                        DateRangeChanged(_dateRange.Mode, _dateRange.CurrentDate);
                    }
                }
            }
        }

        private void _ctlBtnYear_Click(object sender, RoutedEventArgs e)
        {
            if (ClickYear != null)
            {
                ClickYear(_dateRange);
            }
        }
    }
}
